//

import UIKit
//import CLToast

class WP_MonthlyAddViewController: WP_BasicViewController {
    
    var finishStr:String!       //完成
    var UnfinishedStr:String!   //未完成
    var summaryStr:String!      //总结
    var planStr:String!         //计划
    var remarksStr:String!      //备注
    var model:MonthModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitle("MonthlyAdd")
        // Do any additional setup after loading the view.
        
        self.view.addSubview(mTableView)
        setFootView()
        
        WP_CoreDataModel.coreDataManager.setEntityName(text: "MonthModel")
    }
    
    lazy var mTableView:UITableView = {
        
        let tableview = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kNavBarHeight), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.rowHeight = kRatioHeight(100)
        tableview.tableFooterView = UIView()
        tableview .register(WP_FieldTableViewCell.classForCoder(), forCellReuseIdentifier:NSStringFromClass(WP_FieldTableViewCell.classForCoder()))
        tableview.separatorColor = UIColor.white
        return tableview
        
    }()
    
    
    
    //footview
    func setFootView()  {
        
        let foot = WP_FootView(frame: CGRect(x: 0, y: kScreenHeight-kTabBarHeight, width: kScreenWidth, height: kTabBarHeight))
        self.view.addSubview(foot)
        weak var weakSelf = self
        foot.FootBlock = {
            
            if weakSelf!.finishStr?.count==0 ||  weakSelf!.UnfinishedStr?.count==0 || weakSelf!.summaryStr?.count==0 ||
                weakSelf!.planStr?.count==0{
//                CLToast.cl_show(msg: "The input box cannot be empty")
                showMessage("The input box cannot be empty", self.view)
                return
            }
            var dic = Dictionary<String,Any>()
            //            let dic2 = Dictionary<String,Any>()
            
            let dateformatter = DateFormatter()
            dateformatter.dateFormat = "YYYY-MM-dd HH:mm"// 自定义时间格式
            let time = dateformatter.string(from: Date())
            //
            dic.updateValue(weakSelf!.finishStr ?? "", forKey: "finish")
            dic.updateValue(weakSelf!.UnfinishedStr ?? "", forKey: "unfinish")
            dic.updateValue(weakSelf!.summaryStr ?? "", forKey: "summary")
            dic.updateValue(weakSelf!.remarksStr ?? "", forKey: "remark")
            dic.updateValue(weakSelf!.planStr ?? "", forKey: "plan")
            
            if weakSelf!.model == nil {//添加
                
                dic.updateValue(time, forKey: "date")
                dic.updateValue(weakSelf!.randomNum(), forKey: "iD")
                
             WP_CoreDataModel.coreDataManager.saveData(parameter: dic, success: { weakSelf?.navigationController?.popViewController(animated: true)}, fail: {})
                
            }else {//修改
                
               WP_CoreDataModel.coreDataManager.updateData(parameter: dic, key: "iD=\(weakSelf!.model?.iD! ?? "")", success: { weakSelf?.navigationController?.popViewController(animated: true)}, fail: {})
                
            }
        }
    }
    
    
}


extension WP_MonthlyAddViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:WP_FieldTableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(WP_FieldTableViewCell.classForCoder()), for: indexPath) as! WP_FieldTableViewCell
        cell.index = indexPath.row
        cell.selectionStyle = .none
        cell.delegate = self;
        if indexPath.row == 0 {
            cell.titleLabel.text = "Work done month"
            cell.tfield.placeholder = "Input a Work done month"
            cell.tfield.text = finishStr
            
        }else if indexPath.row == 1 {
            cell.titleLabel.text = "Unfinished business month"
            cell.tfield.placeholder = "Input a Unfinished business month"
            cell.tfield.text = UnfinishedStr
            
        }else if indexPath.row == 2 {
            cell.titleLabel.text = "Job summary"
            cell.tfield.placeholder = "Input a Job summary"
            cell.tfield.text = summaryStr
            
        }else if indexPath.row == 3 {
            cell.titleLabel.text = "Work plan for next month"
            cell.tfield.placeholder = "Input a Work plan for next month"
            cell.tfield.text = planStr
            
        }else if indexPath.row == 4 {
            cell.titleLabel.text = "Remarks"
            cell.tfield.placeholder = "Input a Remarks"
            cell.tfield.text = remarksStr
        }
        
        return cell
    }
    
    //
    func setvalue() {
        
        if model != nil {
            finishStr = model?.finish ?? ""
            UnfinishedStr = model?.unfinish ?? ""
            summaryStr = model?.summary ?? ""
            planStr = model?.plan ?? ""
            remarksStr = model?.remark ?? ""
        }else{
            finishStr = ""
            UnfinishedStr = ""
            summaryStr = ""
            planStr = ""
            remarksStr = ""
        }
    }
    
    
}


extension WP_MonthlyAddViewController:WP_FieldTableViewDelegate {
    
    func SendIndexCell(index: NSInteger, obj: String) {
        switch index {
        case 0:
            finishStr = obj
        case 1:
            UnfinishedStr = obj
        case 2:
            summaryStr = obj
        case 3:
            planStr = obj
        case 4:
            remarksStr = obj
        default: break
            
        }
    }
    
    
    
}
